package com.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.core.exception.ParameterException;
import com.demo.dao.NewsDao;
import com.demo.domain.News;
import com.demo.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsDao, News> implements NewsService {
    @Autowired
    private NewsDao newsDao;

    /**
     * 得到新闻列表（可分页可不分页）
     *
     * @param current
     * @param size
     * @return
     * @throws Exception
     */
    @Override
    public Page getDisplayNewsList(Integer current, Integer size, Integer type) throws Exception {
        Page page = new Page<>(current, size);
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据发布时间降序排序
        lambdaQueryWrapper.orderByDesc(News::getReleaseTime);
        //若type为空，默认type=0
        int selectType;
        selectType = (type == null || type < 0 || type > 3 ? 0 : type);
        lambdaQueryWrapper.eq(News::getType, selectType);
        //分页
        if (current != null && size != null && current > 0 && size > 0) {
            return newsDao.selectPage(page, lambdaQueryWrapper);
        }
        //不分页
        List<News> list = newsDao.selectList(lambdaQueryWrapper);
        if (list != null && list.size() > 0) {
            list.stream().forEach(news -> {
                news.setImageUrl(null);
            });
        }
        page.setRecords(list);
        return page;
    }

    /**
     * 得到新闻的详情内容
     *
     * @param newsId
     * @return
     */
    @Override
    public News getNewsContent(Long newsId) throws ParameterException {
        if (newsId <= 0L) {
            throw new ParameterException("newsId值非法");
        }
        News news = newsDao.selectById(newsId);
        String content = news.getContent();
//        从获取的新闻对象中提取了图片URL的JSON表示并将其存储在 imageUrlsJson 变量中。
        String imageUrlsJson = news.getImageUrl();
//        将从新闻对象中提取的图片URL的JSON表示解析为一个字符串列表 imageUrls
        List<String> imageUrls = JSON.parseArray(imageUrlsJson, String.class);
//        检查是否存在图片URL
        if (imageUrls != null && !imageUrls.isEmpty()) {
            //存在图片url则将其加入到imgList中
            imageUrls.stream().forEach(img->{
                news.getImgList().add(img);
            });
//            // 如果新闻包含图片URLs，将图片URLs插入到content的末尾
//            for (String imageUrl : imageUrls) {
//                content += "<br/><img src='" + imageUrl + "'/>";
//            }
        }
//        news.setImgList(content);
//        news.setContent(content);
        return news;
    }

    @Override
    public List<News> getLateSixFriendsNew() {
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按发布时间降序排序
        lambdaQueryWrapper.orderByDesc(News::getReleaseTime);
        //只获取系友新闻
        lambdaQueryWrapper.eq(News::getType, 2);
        //通过分页实现获取前六条数据
        Page page = new Page<>(1, 6);
        newsDao.selectPage(page, lambdaQueryWrapper);
        List<News> newsList = page.getRecords();
        if (newsList != null && newsList.size() > 0) {
            newsList.stream().forEach(news -> {
                news.setImageUrl(null);
            });
        }
        return newsList != null && newsList.size() > 0 ?
                newsList : null;
    }
}
